using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using CommonBehaviors.Actions;

using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using TreeSharp;

namespace FelMaster.Classes.Rogue
{
    class Subltey : TalentBase
    {
        public override string Name
        {
            get { return "Subltey V2.0"; }
        }

        public override string KeySpell
        {
            get { return "Shadowstep"; }
        }

        public bool Debuff(String debuffname)
        {
            return Me.CurrentTarget.Auras.ContainsKey(debuffname);
        }

        public bool BleedCheck()
        {
            return (Debuff("Mangle") || Debuff("Trauma") || Debuff("Gore") || Debuff("Stampede") || Debuff("Tendon Rip") || Debuff("Hemorrhage"));
        }

        public bool IsBehind(WoWUnit target)
        {
            return WoWMathHelper.IsBehind(StyxWoW.Me.Location, target.Location, target.Rotation, (float)Math.PI*5/6);
        }
        
        public override Composite SingleRotation
        {
            get {
                return new PrioritySelector(
                    #region Stealth
                    new Decorator(
                        ret => SC.PlayerHasBuff("Vanish") || SC.PlayerHasBuff("Stealth"),
                        new PrioritySelector(
                            SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                            SC.CastSpell("Shadowstep", a => true, "Shadowstep"),
                            SC.CastSpell("Ambush", a => Me.ComboPoints < 4, "Ambush"),
                            SC.CastSpell("Premeditation", a => Me.ComboPoints <=3, "Premedition"),
                            SC.CastSpell("Rupture", a => !SC.TargetHasDebuff("Rupture") && Me.ComboPoints == 5 && SC.PlayerHasBuff("Master of Subtlety"), "Rupture"),
                            SC.CastSpell("Slice and Dice", a => SC.PlayerBuffTimeLeft("Slice and Dice") < 3 && Me.ComboPoints == 5, "Slice and Dice"),
                            SC.CastSpell("Rupture", a => !SC.TargetHasDebuff("Rupture") && Me.ComboPoints == 5, "Rupture"),
                            SC.CastSpell("Recuperate", a => SC.PlayerBuffTimeLeft("Recuperate") < 3 && Me.ComboPoints == 5, "Recuperate"),
                            SC.CastSpell("Eviscerate", a => Me.ComboPoints == 5 && SC.TargetHasDebuff("Rupture"), "Eviscerate")
                        )
                    ),
                    #endregion
                    #region Shadow Dance
                    new Decorator(
                        ret => SC.PlayerHasBuff("Shadow Dance"),
                        new PrioritySelector(
                            SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                            SC.CastSpell("Shadowstep", a => true, "Shadowstep"),
                            SC.CastSpell("Arcane Torrent", a => Me.CurrentEnergy < 35, "Arcane Torrent"),
                            SC.CastSpell("Ambush", a => Me.ComboPoints < 4, "Ambush"),
                            SC.CastSpell("Premeditation", a => Me.ComboPoints <=3, "Premedition"),
                            SC.CastSpell("Rupture", a => !SC.TargetHasDebuff("Rupture") && Me.ComboPoints == 5 && SC.PlayerHasBuff("Master of Subtlety"), "Rupture"),
                            SC.CastSpell("Slice and Dice", a => SC.PlayerBuffTimeLeft("Slice and Dice") < 3 && Me.ComboPoints == 5, "Slice and Dice"),
                            SC.CastSpell("Rupture", a => !SC.TargetHasDebuff("Rupture") && Me.ComboPoints == 5, "Rupture"),
                            SC.CastSpell("Recuperate", a => SC.PlayerBuffTimeLeft("Recuperate") < 3 && Me.ComboPoints == 5, "Recuperate"),
                            SC.CastSpell("Eviscerate", a => Me.ComboPoints == 5 && SC.TargetHasDebuff("Rupture"), "Eviscerate")
                        )
                    ),
                    #endregion
                    SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                    SC.CastSpell("Blood Fury", a => true, "Blood Fury"),
                    SC.CastSpell("Lifeblood", a => true, "Lifeblood"),
                    SC.CastSpell("Berserking", a => true, "Berserking"),
                    SC.CastSpell("Shadow Dance", a => Me.CurrentEnergy >= 70 && Me.ComboPoints < 3 && !SC.PlayerHasBuff("Stealth") &&SC.CanCast ("Shadowstep"), "Shadow Dance"),
                    SC.CastSpell("Vanish", a => Me.CurrentEnergy >= 60 && Me.ComboPoints < 3 && SC.CanCast ("Shadowstep") && !SC.PlayerHasBuff ("Shadow Dance") && !SC.PlayerHasBuff ("Master of Subtlety") && !SC.TargetHasDebuff("Find Weakness"), "Vanish"),
                    SC.CastSpell("Preparation", a => !SC.CanCast ("Vanish"), "Preparation"),
                    SC.CastSpell("Rupture", a => !SC.TargetHasDebuff("Rupture") && Me.ComboPoints == 5 && SC.PlayerHasBuff("Master of Subtlety"), "Rupture"),
                    SC.CastSpell("Slice and Dice", a => SC.PlayerBuffTimeLeft("Slice and Dice") < 3 && Me.ComboPoints == 5, "Slice and Dice"),
                    SC.CastSpell("Rupture", a => !SC.TargetHasDebuff("Rupture") && Me.ComboPoints == 5, "Rupture"),
                    SC.CastSpell("Recuperate", a => SC.PlayerBuffTimeLeft("Recuperate") < 3 && Me.ComboPoints == 5, "Recuperate"),
                    SC.CastSpell("Eviscerate", a => Me.ComboPoints == 5 && SC.TargetHasDebuff("Rupture"), "Eviscerate"),
                    SC.RunMacroText("/cast [@focus] Tricks of the Trade", a => SC.CanCast("Tricks of the Trade") && StyxWoW.Me.FocusedUnitGuid != 0 && !StyxWoW.Me.FocusedUnit.Dead && StyxWoW.Me.FocusedUnit.Attackable && StyxWoW.Me.FocusedUnit.InLineOfSight && !StyxWoW.Me.FocusedUnit.Mounted, "Tricks of the Trade"),
                    SC.CastSpell("Hemorrhage", a => Me.ComboPoints < 5 && Me.CurrentEnergy > 40 && !BleedCheck(), "Hemorrhage"),
                    SC.CastSpell("Hemorrhage", a => Me.ComboPoints < 5 && !IsBehind(Me.CurrentTarget), "Hemorrhage"),
                    SC.CastSpell("Backstab", a => Me.ComboPoints < 5 && (SC.PlayerBuffTimeLeft("Recuperate") < 4 || SC.PlayerBuffTimeLeft("Slice and Dice") < 4 || SC.TargetDebuffTimeLeft("Rupture") < 4), "Backstab"),
                    SC.CastSpell("Backstab", a => Me.ComboPoints < 2 && Me.CurrentEnergy > 40, "Backstab+"),
                    SC.CastSpell("Backstab", a => Me.ComboPoints < 3 && Me.CurrentEnergy > 50, "Backstab++"),
                    SC.CastSpell("Backstab", a => Me.ComboPoints < 5 && Me.CurrentEnergy > 60, "Backstab+++")
                );
            }
        }

        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector(
                    SC.CastBuff("Stealth", a => !Me.Mounted, "Stealth")
                );
            }
        }

        public override Composite PVPRotation
        {
            get { throw new NotImplementedException(); }
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }
    }
}